package com.symaster.common.swing.element;

import com.symaster.common.swing.KeyStatus;
import com.symaster.common.swing.controller.DragController;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @author yinmiao
 * @version 2022-11-21 15:47
 */
public class Layered extends KeyStatus {

    private final KeyStatus component;

    public Layered(KeyStatus component) {
        this.component = component;
        setLayout(null);
        add(component);
        initListener(component);
        updateUI();
    }

    private void initListener(KeyStatus component) {

        addMouseWheelListener(e -> {
            Rectangle bounds = component.getBounds();
            if (!component.leftShiftPressed) {
                if (e.getWheelRotation() > 0) {
                    component.setBounds(bounds.x, bounds.y - 20, bounds.width, bounds.height);
                } else {
                    component.setBounds(bounds.x, bounds.y + 20, bounds.width, bounds.height);
                }
            } else {
                if (e.getWheelRotation() > 0) {
                    component.setBounds(bounds.x - 20, bounds.y, bounds.width, bounds.height);
                } else {
                    component.setBounds(bounds.x + 20, bounds.y, bounds.width, bounds.height);
                }
            }
        });

        DragController d1 = new DragController(component);
        d1.addDragListener(DragController.DRAG_TYPE_CENTER, (s, e) -> {
            int offsetX = e.x - s.x;
            int offsetY = e.y - s.y;
            Rectangle bounds = component.getBounds();
            component.setBounds(bounds.x += offsetX, bounds.y += offsetY, component.getWidth(), component.getHeight());
        });
        DragController d2 = new DragController(this);
        d2.setStartListener(DragController.DRAG_TYPE_CENTER, point -> {
            Rectangle bounds = component.getBounds();
            return new Point(point.x - bounds.x, point.y - bounds.y);
        });
        d2.addDragListener(DragController.DRAG_TYPE_CENTER, (s, e) -> {
            Rectangle bounds = component.getBounds();
            int offsetX = e.x - bounds.x - s.x;
            int offsetY = e.y - bounds.y - s.y;
            component.setBounds(bounds.x += offsetX, bounds.y += offsetY, component.getWidth(), component.getHeight());
        });
    }

    @Override
    public void paint(Graphics g) {
        Rectangle bounds1 = component.getBounds();
        component.setBounds(bounds1.x, bounds1.y, component.getWidth(), component.getHeight());
        Color background = getBackground();
        if (background != null) {
            g.setColor(background);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        super.paint(g);
        g.setColor(new Color(63, 63, 63, 200));
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        g.setColor(new Color(63, 63, 63, 100));
        g.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
    }
}
